const app = getApp();
import grace from "../../../utils/grace.js";

grace.page({

  /**
   * 页面的初始数据
   */
  data: {
    ellipsis: true, // 文字是否收起，默认收起
    banners: [],
    categories: [],
    posters: [],
    total: 0,
    page: 1,
    pagesize: 10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    let school_id = wx.getStorageSync('school_id')

    // 加载圈子分类
    let url = app.globalData.api + '/api/v1.0/posterCategory/' + school_id;
    that.$http.get(url).then((res) => {
      if (res.data.code == 0) {
        that.$data.categories = res.data.data;
      }
    }).catch(err => {
      console.log(err.status, err.message);
    })
  },
  getList() {
    let that = this;
    let school_id = wx.getStorageSync('school_id')
    wx.showLoading({
      title: '加载中...',
    })
    let url = app.globalData.api + '/api/v1.0/posterList/' + school_id + '/0' + '?page=' + that.$data.page + '&pagesize=' + that.$data.pagesize;
    that.$http.get(url).then((res) => {
      if (res.data.code == 0) {
        that.$data.total = res.data.data.total;
        that.$data.page = that.$data.page + 1;
        that.$data.posters = that.$data.posters.concat(res.data.data.list);
      }
      wx.hideLoading();
    }).catch(err => {
      console.log(err.status, err.message);
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    let school_id = wx.getStorageSync('school_id')

    // 加载banner
    let url = app.globalData.api + '/api/v1.0/banners/' + school_id + '/circle_index';
    that.$http.get(url).then((res) => {
      if (res.data.code == 0) {
        that.$data.banners = res.data.data;
      }
    }).catch(err => {
      console.log(err.status, err.message);
    })

    // 加载最新帖子
    that.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if(this.$data.posters.length < this.$data.total){
      this.getList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 收起/展开按钮点击事件
   */
  ellipsis: function() {
    var value = !this.data.ellipsis;
    this.setData({
      ellipsis: value
    })
  },

  previewImage: function (e) {
    let imageUrls = [];
    for (let i = 0; i < e.currentTarget.dataset.images.length; i++) {
      imageUrls.push(e.currentTarget.dataset.images[i].pic_url);
    }
    wx.previewImage({
      current: imageUrls[0].pic_url,
      urls: imageUrls
    })
  },
})